//index.js
//获取应用实例
var app = getApp();

Page({
    data: {
        autoplay: true,
        interval: 3000,
        duration: 1000,
        goodsDetail: {},
        swiperCurrent: 0,
        hasMoreSelect: false,
        selectSize: "选择：",
        selectSizePrice: 0,
        shopNum: 0,
        hideShopPopup: true,
        buyNumber: 1,
        buyNumMin: 1,
        buyNumMax: 100000,
        childs: 0,
        gooddetaillist: [],
        propertyChildIds: "",
        propertyChildNames: "",
        canSubmit: false, //  
        shopCarInfo: {},
        data: null,
        images: {},
        shopType: "addShopCar", //
        categories: [{
                id: 0,
                name: "颜色",
                active: true
            },

        ],
        categories1: [


        ],
        spot: ''
    },


    containerTap: function (res) {
        var that = this
        var x = res.touches[0].pageX;
        var y = res.touches[0].pageY + 85;
        this.setData({
            rippleStyle: ''
        });
        setTimeout(function () {
            that.setData({
                rippleStyle: 'top:' + y + 'px;left:' + x + 'px;-webkit-animation: ripple 0.4s linear;animation:ripple 0.4s linear;'
            });
        }, 200)
    },


    imageLoad: function (e) {
        var $width = e.detail.width, //获取图片真实宽度
            $height = e.detail.height,
            ratio = $width / $height; //图片的真实宽高比例
        var viewWidth = 750, //设置图片显示宽度，左右留有16rpx边距
            viewHeight = 718 / ratio; //计算的高度值
        var image = this.data.images;
        //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
        image[e.target.dataset.index] = {
            width: viewWidth,
            height: viewHeight
        }
        this.setData({
            images: image
        })
    },





    //事件处理函数
    swiperchange: function (e) {
        //console.log(e.detail.current)
        this.setData({
            swiperCurrent: e.detail.current
        })
    },
    onLoad: function (e) {
        console.log("id:" + e.id)
        var that = this;
        // 获取景点数据
        var categories1 = [];
        wx.request({
            url: app.globalData.headurl + '/Spot/selectSpotById',
            data: {
                id: e.id
            },
            success: function (res) {
                that.data.spot = res.data;
                that.setData({

                    food: res.data,
                    categories1: categories1,
                    childs: 0,
                    isdetail: 1

                });

            }
        })

     

        wx.request({
            url: app.globalData.headurl + '/comment/selectCommentBybid',
            data: {
                bid: e.id
            },
            success: function (res) {

                console.log("comment:" + res.data.length)
                that.setData({

                    commentlist: res.data

                });

            }
        })
    },


    submitForm: function (e) {
        var that=this;
        var form = e.detail.value;
        wx.request({
            url: app.globalData.headurl + '/comment/addcomment',
            data: {
                headimgurl: 'https://img2.baidu.com/it/u=29885366,3592956385&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1675875600&t=0875c67fa1f53005b5c3ced7ac4b1147',
                nickname: '游客',//想使用globalData的customer的，但是有问题使用不了。。。。
                content: form.comment,
                openid: app.globalData.openid,
                bid: that.data.spot.id,
            },
            success: function (res) {
                wx.showToast({
                  title: '评论成功',
                })
                that.setData({
                    commentlist:res.data
                })
            }
        })
    },

    toaddCfood: function (e) {
        var fid = e.currentTarget.dataset.fid;
        var name = e.currentTarget.dataset.name;
        var price = e.currentTarget.dataset.price;
        var logo = e.currentTarget.dataset.logo
        var that = this
        console.log("收藏：")
        wx.request({
            url: app.globalData.headurl + '/qcfood/addcfood',
            data: {
                name: name,
                price: price,
                logo: logo,
                fid: fid,
                openid: app.globalData.openid
            },
            method: 'POST',
            header: {
                //'content-type': 'application/json' // 默认值 
                'content-type': 'application/x-www-form-urlencoded' // 默认值 
            },
            success: function (res) {
                that.setData({
                    icf: 1
                })
                wx.showToast({
                    title: '收藏成功',
                })
            }
        })


    },

    todeleteCfood: function (e) {
        var id = e.currentTarget.dataset.id;
        var that = this
        wx.request({
            url: app.globalData.headurl + '/qcfood/deletecfood',
            data: {

                id: id
            },
            method: 'POST',
            header: {
                //'content-type': 'application/json' // 默认值 
                'content-type': 'application/x-www-form-urlencoded' // 默认值 
            },
            success: function (res) {
                that.setData({
                    icf: 0
                })
                wx.showToast({
                    title: '取消成功',
                })
            }
        })
    },

    goShopCar: function () {
        wx.reLaunch({
            url: "/pages/shop-cart/index"
        });
    },
    toAddShopCar: function () {
        this.setData({
            shopType: "addShopCar"
        })
        this.bindGuiGeTap();
    },
    tobuy: function () {
        this.setData({
            shopType: "tobuy"
        });
        this.bindGuiGeTap();

    },
    /**
     * 规格选择弹出框
     */
    bindGuiGeTap: function () {
        this.setData({
            hideShopPopup: false
        })
    },
    /**
     * 规格选择弹出框隐藏
     */
    closePopupTap: function () {
        this.setData({
            hideShopPopup: true
        })
    },
    numJianTap: function () {
        if (this.data.buyNumber > this.data.buyNumMin) {
            var currentNum = this.data.buyNumber;
            currentNum--;
            this.setData({
                buyNumber: currentNum
            })
        }
    },
    numJiaTap: function () {

        if (this.data.buyNumber < this.data.buyNumMax) {
            var currentNum = this.data.buyNumber;
            currentNum++;
            this.setData({
                buyNumber: currentNum
            })
        }
    },


    /**
     * 组建立即购买信息
     */
    buliduBuyNowInfo: function () {
        var shopCarMap = {};
        shopCarMap.goodsId = this.data.food.id;
        shopCarMap.pic = this.data.goodsDetail.basicInfo.pic;
        shopCarMap.name = this.data.goodsDetail.basicInfo.name;
        // shopCarMap.label=this.data.goodsDetail.basicInfo.id; 规格尺寸 
        shopCarMap.propertyChildIds = this.data.propertyChildIds;
        shopCarMap.label = this.data.propertyChildNames;
        shopCarMap.price = this.data.selectSizePrice;
        shopCarMap.left = "";
        shopCarMap.active = true;
        shopCarMap.number = this.data.buyNumber;
        shopCarMap.logisticsType = this.data.goodsDetail.basicInfo.logisticsId;
        shopCarMap.logistics = this.data.goodsDetail.logistics;
        shopCarMap.weight = this.data.goodsDetail.basicInfo.weight;

        var buyNowInfo = {};
        if (!buyNowInfo.shopNum) {
            buyNowInfo.shopNum = 0;
        }
        if (!buyNowInfo.shopList) {
            buyNowInfo.shopList = [];
        }

        buyNowInfo.shopList.push(shopCarMap);
        return buyNowInfo;
    },
    onShareAppMessage: function () {
        return {
            title: this.data.goodsDetail.basicInfo.name,
            path: '/pages/goods-details/index?id=' + this.data.goodsDetail.basicInfo.id + '&inviter_id=' + app.globalData.uid,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },

    onShow: function () {
        var that = this
        that.setData({
            imgsrc: app.globalData.headurl,
        })
    },
    // 页面渲染完成
    onReady: function () {
        var circleCount = 0;
        // 心跳的外框动画
        this.animationMiddleHeaderItem = wx.createAnimation({
            duration: 1000, // 以毫秒为单位

            timingFunction: 'linear',
            delay: 100,
            transformOrigin: '50% 50%',
            success: function (res) {}
        });

        setInterval(function () {
            if (circleCount % 2 == 0) {
                this.animationMiddleHeaderItem.scale(1.15).step();
            } else {
                this.animationMiddleHeaderItem.scale(1.0).step();
            }

            this.setData({
                animationMiddleHeaderItem: this.animationMiddleHeaderItem.export()
            });

            circleCount++;
            if (circleCount == 1000) {
                circleCount = 0;
            }
        }.bind(this), 1000);

    },

    toGroupBuying: function (e) {
        var that = this;
        var gnum = that.data.buyNumber
        var glogo = that.data.spot.flogo
        var gid = that.data.spot.id
        var gname = that.data.spot.fname
        var gprice = that.data.spot.fprice
        console.log("gnum:" + gnum)

        if (this.data.buyNumber < 1) {
            wx.showModal({
                title: '提示',
                content: '购买数量不能为0！',
                showCancel: false
            })
            return;
        }


        var shopCarMap = {};
        shopCarMap.cart_goodid = gid;
        shopCarMap.cart_goodlogo = glogo;
        shopCarMap.cart_goodname = gname;
        shopCarMap.cart_goodcount = gnum;
        shopCarMap.cart_goodprice = gprice;
        shopCarMap.id = ''
        var buyNowInfo = {};

        buyNowInfo.shopList = [];

        buyNowInfo.shopList.push(shopCarMap);

        console.log("cartgoodname:" + buyNowInfo.shopList.length)
        wx.setStorage({
            key: "buyNowInfo",
            data: buyNowInfo
        })

        this.closePopupTap()
        wx.navigateTo({
            url: "/pages/topay/index?orderType=buyNow&ifc=门票"
        })

    },

})